package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 *  问题3：
	同问题2，请按照小时+15分钟组装数据，进行转换，
	如把小时数据和小时对应的30分钟数据组装如下的Map，
	如<0115，21>，<0130，13>，<0145，34>，<0160，78>，以此类推，
	对结果进行打印，
	并对这个Map按照整数Value从大到小排序，返回排序后的Map，进行打印。
 */

public class QuartersDemo {
	public static void main(String[] args) throws Exception {
		//从data.txt读入数据放入list集合中
		ArrayList<String> list = getList();
        
        //处理list集合，获取小时、15分钟数据的集合
        ArrayList<String> hours = getHoursData(list);
        ArrayList<String> quartersData = getQuarters(list);
        //System.out.println(halfHoursData);
        
        //处理hours集合，创建符合条件的key值的集合
        ArrayList<String> key2 = new ArrayList<>();
        for(int i=0;i<32;i++){
        	if(i%4==0){
        		key2.add("0"+hours.get(i/4)+"15");
        	}else if(i%4==1){
        		key2.add("0"+hours.get(i/4)+"30");
        	}else if(i%4==2){
        		key2.add("0"+hours.get(i/4)+"45");
        	}else{
        		key2.add("0"+hours.get(i/4)+"60");
        	}
        }
        //System.out.println(key2);
        
        //创建map集合，放入15分钟数据
        Map<String, String> map =new HashMap<String, String>();
        for(int i=0;i<key2.size();i++){
        	map.put(key2.get(i), quartersData.get(i));
        }
        
        //打印输出15分钟数据
        System.out.println("15分钟排序数据：");
        List<Map.Entry<String, String>> list1 = new ArrayList<>(map.entrySet());
        list1.sort((e1, e2) -> Integer.compare(Integer.parseInt(e2.getValue()), Integer.parseInt(e1.getValue())));
        for (Map.Entry<String, String> entry : list1) {  
            System.out.println("<" + entry.getKey() + "," + entry.getValue() + ">");  
        } 
	}
	
	//getList
	private static ArrayList<String> getList() throws Exception {
		ArrayList<String> list = new ArrayList<>();
		BufferedReader br = new BufferedReader(new FileReader("data.txt"));
		String line;  
        while ((line = br.readLine()) != null) {  
            list.add(line);
        } 
        br.close();
        //System.out.println(list);
		return list;
	}

	//getHoursData
	private static ArrayList<String> getHoursData(ArrayList<String> list) {
		ArrayList<String> hours = new ArrayList<>();
		String str=list.get(1);
        //分离";"
        for(int i=0;i<8;i++){
        	String[] split= str.split(";");
        	hours.add(split[i]);
        }
        //System.out.println(hours);
		return hours;
	}
	//getQuarters
	private static ArrayList<String> getQuarters(ArrayList<String> list) {
		ArrayList<String> quarters1= new ArrayList<>();
		ArrayList<String> quarters = new ArrayList<>();
		//取15分钟字串
		String str=list.get(5);
		//System.out.println(str);
		//分离";"
        for(int i=0;i<8;i++){
        	String[] split= str.split(";");
        	quarters1.add(split[i]);
        }
      //取data
        Pattern pattern = Pattern.compile(":(\\d+),");  
        Matcher matcher = pattern.matcher(quarters1.toString());  
        while (matcher.find()) {  
            String number = matcher.group(1);
            quarters.add(number);
        }
       // System.out.println(quarters1.toString());
        String[] split=quarters1.toString().split(":");
        String last = split[32].substring(0,split[32].length()-1);
        quarters.add(last);
      //处理数据，若为个位数，则前面加0
        for(int i=0;i<quarters.size();i++){
        	if(quarters.get(i).length()==1){
        		quarters.set(i, "0".concat(quarters.get(i)));
        	}
        }
        //System.out.println(quarters);
		return quarters;
	}
}
